<template>
	<Dialog title="选择用户" v-model="dialogVisible" width="60%">
		<ContentWrap>
			<!-- 搜索工作栏 -->
			<el-form ref="queryFormRef" :inline="true" :model="queryParams" class="-mb-15px" label-width="68px">
				<el-form-item label="用户昵称" prop="nickname">
					<el-input v-model="queryParams.nickname" class="!w-240px" clearable placeholder="请输入用户昵称"
						@keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="手机号" prop="mobile">
					<el-input v-model="queryParams.mobile" class="!w-240px" clearable placeholder="请输入手机号"
						@keyup.enter="handleQuery" />
				</el-form-item>

				<el-form-item>
					<el-button @click="handleQuery">
						<Icon class="mr-5px" icon="ep:search" />
						搜索
					</el-button>
					<el-button @click="resetQuery">
						<Icon class="mr-5px" icon="ep:refresh" />
						重置
					</el-button>
					<el-button v-hasPermi="['promotion:coupon:send']" @click="openCoupon">发送优惠券</el-button>
				</el-form-item>
			</el-form>
		</ContentWrap>

		<!-- 列表 -->
		<ContentWrap>
			<el-table ref="tableDataRefs" v-loading="loading" :data="list" :show-overflow-tooltip="true" :stripe="true"
				@selection-change="handleSelectionChange" :header-cell-class-name="hideAllCheckbox">
				<el-table-column type="selection" width="55" />
				<el-table-column align="center" label="用户编号" prop="id" width="120px" />
				<el-table-column align="center" label="头像" prop="avatar" width="80px">
					<template #default="scope">
						<img :src="scope.row.avatar" style="width: 40px" />
					</template>
				</el-table-column>
				<el-table-column align="center" label="手机号" prop="mobile" width="120px" />
				<el-table-column align="center" label="昵称" prop="nickname" />
				<el-table-column align="center" label="状态" prop="status" width="100px">
					<template #default="scope">
						<dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
					</template>
				</el-table-column>
				<el-table-column :formatter="dateFormatter" align="center" label="登录时间" prop="loginDate"
					width="180px" />
				<el-table-column :formatter="dateFormatter" align="center" label="注册时间" prop="createTime"
					width="180px" />

			</el-table>
			<!-- 分页 -->
			<Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNo" :total="total"
				@pagination="getList" />
		</ContentWrap>
		<template #footer>
			<el-button @click="submitForm" type="primary" :loading="formLoading">确 定</el-button>
			<el-button @click="dialogVisible = false">取 消</el-button>
		</template>
	</Dialog>
</template>
<script setup lang="ts">
	import { dateFormatter } from '@/utils/formatTime'
	import * as UserApi from '@/api/member/user'
	import { DICT_TYPE } from '@/utils/dict'
	const props = withDefaults(defineProps<{ isMultiple?:boolean;}>(), {
	  isMultiple:false
	}) 
	const message = useMessage() // 消息弹窗
	import { checkPermi } from '@/utils/permission'
	const formLoading = ref(false)
	const dialogVisible = ref(false) // 弹窗的是否展示
	const loading = ref(false) // 列表的加载中
	const total = ref(0) // 列表的总页数
	const list = ref([]) // 列表的数据
	const queryParams = reactive({
		pageNo: 1,
		pageSize: 10,
		nickname: null,
		mobile: null,
		loginDate: [],
		createTime: [],
		tagIds: [],
		levelId: null,
		groupId: null
	})
const tableDataRefs= ref() 
	const queryFormRef = ref() // 搜索的表单
	const updateLevelFormRef = ref() // 修改会员等级表单
	const updatePointFormRef = ref() // 修改会员积分表单
	const UpdateBalanceFormRef = ref() // 修改用户余额表单
	const selectedIds = ref<number[]>([]) // 表格的选中 ID 数组
	const hideAllCheckbox = ({ row, column, rowIndex, columnIndex })=>{
		if(!props.isMultiple){
		if (columnIndex === 0) return "setDisable";
		}else{
			return ""
		}
	}
	/** 查询列表 */
	const getList = async () => {
		loading.value = true
		try {
			const data = await UserApi.getUserPage(queryParams)
			list.value = data.list
			total.value = data.total
		} finally {
			loading.value = false
		}
	}

	/** 搜索按钮操作 */
	const handleQuery = () => {
		if(!queryParams.nickname&&!queryParams.mobile){
			message.error("请输入用户昵称或手机号")
			return
		}
		queryParams.pageNo = 1
		getList()
	}

	/** 重置按钮操作 */
	const resetQuery = () => {
		queryFormRef.value.resetFields()
		handleQuery()
	}
	/** 表格选中事件 */
	const handleSelectionChange = (rows : UserApi.UserVO[]) => {
		  if(!props.isMultiple){
			  if (rows.length > 1) {
			    // 如果选择了多个，只保留最后一个
			    selectedIds.value =[rows[rows.length - 1]];
			    let del_row = rows.shift();
			    // 其余的都不选中
			    tableDataRefs.value.toggleRowSelection(del_row, false);
			  } else {
			    selectedIds.value = rows.length ? [rows[0]] : null;
			  }
		  }else{
			 selectedIds.value = rows.map((row) => row) 
		  }
		 
	}

	/** 打开弹窗 */
	const open = async () => {
		dialogVisible.value = true
		// handleQuery()
	}
	const close = async () => {
		dialogVisible.value = false
		selectedIds.value = []
	}
	defineExpose({ open,close }) // 提供 open 方法，用于打开弹窗
	const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
	const submitForm = async () => {
		if (!selectedIds||selectedIds.value.length == 0) {
			message.error("请选择用户")
			return
		}
		// 发送操作成功的事件
		emit('success', selectedIds.value)
	}
</script>
<style lang="scss" scoped>
	:deep(.setDisable) {
		.cell {
			visibility: hidden;
		}
	}
</style>